/*
 * @lc app=leetcode.cn id=5 lang=cpp
 *
 * [5] 最长回文子串
 */

// @lc code=start


class Solution {
public:
    string palindrome(string& s, int a, int b) {
        while (a >= 0 && b < s.length() && s[a] == s[b]) {
            a--;
            b++;
        }
        return s.substr(a+1, b-a-1);
    }
    
    string longestPalindrome(string s) {
        string res = "";
        for (int i = 0; i < s.length(); i++) {
            string s1 = palindrome(s, i, i);
            string s2 = palindrome(s, i, i+1);
            res = res.length() > s1.length() ? res : s1;
            res = res.length() > s2.length() ? res : s2;
        }
        return res;
    }
};
// @lc code=end

